package com.guanghua.brick.html;

import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.el.ELException;
import javax.servlet.jsp.el.ExpressionEvaluator;
import javax.servlet.jsp.el.VariableResolver;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CommonDataSource implements IDataSource {
	
	private static Log logger = LogFactory.getLog(CommonDataSource.class);
	
	public String parseDataSource(PageContext pageContext, String dataSource) {
		//获取el的接口
		ExpressionEvaluator ee = pageContext.getExpressionEvaluator();
		VariableResolver vr = pageContext.getVariableResolver();
		
		//执行
		try {
			return (String)ee.evaluate("${" + dataSource + "}", String.class, vr, null);
		} catch (ELException e) {
			logger.error("el exception", e);
			return null;
		}
	}

	public Object parseDataSource(PageContext pageContext, String dataSource, Class cls) {
		//获取el的接口
		ExpressionEvaluator ee = pageContext.getExpressionEvaluator();
		VariableResolver vr = pageContext.getVariableResolver();
		
		//执行
		try {
			return ee.evaluate("${" + dataSource + "}", cls, vr, null);
		} catch (ELException e) {
			logger.error("el exception", e);
			return null;
		}
	}

}
